package cn.tedu.service;

import cn.tedu.mapper.UserMapper;
import cn.tedu.pojo.User;
import cn.tedu.vo.PageResult;
import cn.tedu.vo.SysResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@Service
public class UserServiceImpl implements UserService{
    @Autowired
    private UserMapper userMapper;

    @Override
    public User addUser(User user) {
        String name = user.getName();
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",name);
        User userDB = userMapper.selectOne(queryWrapper);
        if(userDB!=null){
            return userDB;
        }
        String password = user.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        user.setPassword(password);
        user.setCreatetime(new SimpleDateFormat("yyyy年MM月dd日HH时mm分").format(new Date()));
        userMapper.insert(user);
        return null;
    }

    @Override
    public String login(User user) {
        String md5 = DigestUtils.md5DigestAsHex(user.getPassword().getBytes());
        user.setPassword(md5);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>(user);
        user = userMapper.selectOne(queryWrapper);
        if(user==null){
            return null;
        }
        String token = UUID.randomUUID().toString().replace("-", "");
        return token;
    }

    @Override
    public User queryPhone(User user) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone",user.getPhone());
        user = userMapper.selectOne(queryWrapper);
        return user;
    }

    @Override
    public void resetPassword(User user) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone",user.getPhone());
        user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
        user.setUpdatetime(new SimpleDateFormat("yyyy年MM月dd日HH时mm分").format(new Date()));
        userMapper.update(user,queryWrapper);
    }

    @Override
    public PageResult findUserAll(PageResult pageResult) {
        IPage<User> page = new Page<>();
        page.setSize(pageResult.getPageSize()).setCurrent(pageResult.getPageNum());
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        Boolean flag= StringUtils.hasLength(pageResult.getQuery());
        queryWrapper.like(flag, "name", pageResult.getQuery());
        IPage<User> userIPage = userMapper.selectPage(page, queryWrapper);
        pageResult.setRows(userIPage.getRecords()).setTotal(userIPage.getTotal());
        return pageResult;
    }

    @Override
    public void deleteUser(Integer id) {
        userMapper.deleteById(id);
    }

    @Override
    public void addUser2(User user) {
        String s = DigestUtils.md5DigestAsHex(user.getPassword().getBytes());
        user.setPassword(s);
        user.setCreatetime(new SimpleDateFormat("yyyy年MM月dd日HH时mm分").format(new Date()));
        userMapper.insert(user);
    }

    @Override
    public User selectUser(Integer id) {
        User user = userMapper.selectById(id);
        return user;
    }

    @Override
    public void updateUser(User user) {
        user.setUpdatetime(new SimpleDateFormat("yyyy年MM月dd日HH时mm分").format(new Date()));
        userMapper.updateById(user);
    }
}
